package tabuImpl.tsp.algorithm;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import tabuImpl.tsp.gene.TabuGene;
import tabuImpl.util.TabuUtil;
import tabuInterface.algorithm.TabuSearchNoParameter;
import tabuInterface.algorithm.tabuList.TabuList;
import tabuInterface.algorithm.tabuLogic.TabuLogic;
import tabuInterface.gene.Gene;

import java.util.Arrays;
import java.util.List;

@Component
public class TabuSeachImpl extends TabuSearchNoParameter {
    private double [][] dis;


    public double[][] getDis() {
        return dis;
    }

    public TabuSeachImpl(@Value("#{null}") TabuList tabuList, @Value("1")int iterNum, @Value("1")int tabuNum, @Value("#{null}")TabuLogic pardon, @Value("#{'${tsp.twoArray}'.split(';')}") List<String> dis) {
        super(tabuList,iterNum,tabuNum,pardon);

        String t=dis.get(0);
        int n=t.split(",").length;
        double [][] ans=new double[dis.size()][n];
        int i=0;
        for (String ts:
             dis) {
            ans[i++]=Arrays.stream(ts.split(",")).mapToDouble(Double::parseDouble).toArray();
        }

        this.dis=ans;



    }

    @Override
    public int eval(Gene gene) {
       TabuGene myGene=(TabuGene) gene;
       int []data=myGene.data();
       float sum=0;
       for(int i=0;i<data.length;i++)
           sum+=dis[data[i]][data[(i+1)% data.length]];
       return (int)sum*1000;

    }

    @Override
    public Gene iterInit() {//生成初始解
        int [] data=TabuUtil.randomChoice(dis.length);
        return  new TabuGene(data,this);

    }
}
